<template>
  <div class="user_container">
    <div class="input_box">
      <el-input v-model="username" style="width: 240px" placeholder="用户名" />
      <el-input v-model="name" style="width: 240px" placeholder="昵称" />
      <el-input v-model="phone" style="width: 240px" placeholder="手机号" />
      <el-input v-model="gender" style="width: 240px" placeholder="性别" />
      <el-button @click="search()">查询</el-button>
      <el-button type="primary" @click="addClick()">新增用户</el-button>
    </div>
    <div style="margin-top: 10px">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="ID" width="180" />
        <el-table-column prop="username" label="用户名" width="180" />
        <el-table-column prop="name" label="昵称" />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column prop="gender" label="性别" />
        <el-table-column fixed="right" label="操作" min-width="120">
          <template #default="scope">
            <el-button type="warning" @click="openEdit(scope.row)"> 编辑 </el-button>
            <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <el-dialog
    v-model="centerDialogVisible"
    :title="operation ? '新增' : '编辑'"
    width="500"
    align-center
  >
    <el-form :model="form" label-width="auto" style="max-width: 600px">
      <el-form-item label="用户名">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="用户昵称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="性别">
        <el-input v-model="form.gender" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="operation?add():put()">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { getapi,postapi,putapi,delapi } from "../utils/request";
import router from "../router/index";
export default {
  data() {
    return {
      username: "",
      name: "",
      phone: "",
      gender: "",
      tableData: [],
      centerDialogVisible: false,
      operation: false,
      form: {
        id:"",
        username: "",
        name: "",
        phone: "",
        gender: "",
      },
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      getapi("/user").then((res) => {
        this.tableData = res.data;
      });
    },
    add(){
        this.form.id=this.tableData.length+1
        postapi("/user",this.form).then(res=>{
            this.getData()
            alert("添加成功")
            this.centerDialogVisible=false
        })
    },
    put(){
        console.log(this.form);
        putapi("/user/"+this.form.id,this.form).then(res=>{
            this.getData()
            alert("修改成功")
            this.centerDialogVisible=false
        })
    },
    addClick(){
        this.centerDialogVisible=true
        this.operation=true
        this.form={
        id:"",
        username: "",
        name: "",
        phone: "",
        gender: "",
      }
    },
    openEdit(data){
        this.centerDialogVisible=true
        this.operation=false
        this.form=data
    },
    del(id){
         delapi("/user/"+id).then(res=>{
            this.getData()
            alert("删除成功")
        })
    },
    search(){
        getapi("/user").then((res) => {
            let datas= res.data;
            datas=datas.filter(user=>{
                return user.username.indexOf(this.username)>-1&&user.name.indexOf(this.name)>-1&&user.phone.indexOf(this.phone)>-1&&user.gender.indexOf(this.gender)>-1
            })
            this.tableData=datas
      });
    }
  },
};
</script>

<style scoped>
.user_container {
  background-color: #e9eef3;
  height: 100%;
  box-sizing: border-box;
  padding: 10px;
}
.input_box {
  display: flex;
  gap: 10px;
}
</style>